package com.rattsoftware.client.controllers;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.rattsoftware.client.RefreshableEvent;
import com.rattsoftware.client.RefreshablePublisher;
import com.rattsoftware.client.webclient;
import com.rattsoftware.client.events.AppEvents;
import com.rattsoftware.client.model.RoomSummary;
import com.rattsoftware.client.model.StaffSummary;
import com.rattsoftware.client.model.TestData;
import com.rattsoftware.client.util.ProgressIndicator;
import com.rattsoftware.client.views.RoomView;
import com.rattsoftware.server.admin.model.Room;
import com.rattsoftware.server.admin.model.User;
import com.rattsoftware.server.admin.model.exceptions.RattsParsingException;

public class RoomController extends Controller {

	private RoomView roomView;
	
	public RoomController() {
		registerEventTypes(AppEvents.Init);
		registerEventTypes(AppEvents.RoomView);
	}
	
	@Override
	public void handleEvent(AppEvent event) {
		GWT.log("RoomController : handleEvent");
		EventType type = (EventType) event.getType();
		if (type == AppEvents.Init) {
			forwardToView(roomView, event);
		} else if (type == AppEvents.RoomView) {
			forwardToView(roomView, event);
		}
	}

	public void initialize() {
		super.initialize();
		GWT.log("RoomController : initialize");
		roomView = new RoomView(this);
		
		RefreshablePublisher.get().subscribe(RefreshableEvent.Type.CREATE_ROOM, roomView);
		RefreshablePublisher.get().subscribe(RefreshableEvent.Type.UPDATE_ROOM, roomView);
		RefreshablePublisher.get().subscribe(RefreshableEvent.Type.DELETE_ROOM, roomView);
	}
	
	public void getRoomList(final PagingLoadConfig pagingLoadConfig, 
			final AsyncCallback<com.extjs.gxt.ui.client.data.PagingLoadResult<RoomSummary>> callback)
			throws RattsParsingException {
		GWT.log("RoomController : getRoomList");
		User user = Registry.get(webclient.LOGGED_IN_USER_NAME);
		
		/*courseService.getCourseList(user, PagingUtil.createPagingLoadConfig(pagingLoadConfig),
				new RattsAsyncCallback<PagingLoadResult<Course>>() {
					@Override
					public void onSuccess(PagingLoadResult<Course> result) {*/
						ProgressIndicator.hideProgressBar();
						List<RoomSummary> results = new ArrayList<RoomSummary>();
						//List<Course> data = result.getData();
						List<Room> data = TestData.getRooms();
						
						for (Room f : data) {
							results.add(new RoomSummary(f));
						}
						roomView.setAllRoomSummaries(results); // reset the list
						callback.onSuccess(new BasePagingLoadResult<RoomSummary>(results, 0, 20));
						/*callback.onSuccess(new BasePagingLoadResult<CourseSummary>(results, result.getOffset(), result
								.getTotalLength()));
					}
				});*/
	}
	
	public void forwardToNewRoomController() {
		GWT.log("RoomController : forwardToNewRoomController");
		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.dispatch(AppEvents.NewRoom);
	}
	
	public void forwardToEditRoomController(RoomSummary roomSummary) {
		GWT.log("RoomController : forwardToEditRoomController");
		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.dispatch(AppEvents.EditRoom, roomSummary);
	}
}
